import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:netease_cloud_music_api/netease_cloud_music_api.dart';
import 'package:netease_cloud_music_api/utils/crypto.dart';
import 'package:netease_cloud_music_api/utils/player_request.dart';
import 'package:netease_cloud_music_api/utils/request.dart';

mixin Other {
  /// Banner
  /// 说明 : 调用此接口 , 可获取 banner( 轮播图 ) 数据
  Future<PlayerRequestResponse> banner({ ClientType? clientType }) {
    clientType = clientType ?? ClientType.iphone;
    Map<String, dynamic> data = Map<String, dynamic>();
    if (clientType == ClientType.iphone) data['clientType'] = 'iphone';
    if (clientType == ClientType.android) data['clientType'] = 'android';
    if (clientType == ClientType.ipad) data['clientType'] = 'ipad';
    if (clientType == ClientType.pc) data['clientType'] = 'pc';
    return PlayerRequest(
      url: 'https://music.163.com/eapi/banner/get/v3',
      data: data,
      options: PlayerRequestOptions(
        method: PlayerRequestMethod.post,
        crypto: EncryptType.weapi,
        url: '/api/banner/get/v3',
      ),
    ).request();
  }

  /// 音乐日历
  /// 说明 : 登录后调用此接口,传入开始和结束时间,可获取音乐日历
  /// [startTime] 开始时间, 默认为当天
  /// [endTime] 开始时间, 默认为当天
  Future<PlayerRequestResponse> calendar({ DateTime? startTime, DateTime? endTime }) {
    startTime = startTime ?? DateTime.now();
    endTime = endTime ?? DateTime.now();
    return PlayerRequest(
      url: 'https://music.163.com/api/mcalendar/detail',
      data: { 'startTime': startTime.millisecondsSinceEpoch, 'endTime': endTime.millisecondsSinceEpoch },
      options: PlayerRequestOptions(
          method: PlayerRequestMethod.post,
          crypto: EncryptType.weapi,
      ),
    ).request();
  }

  /// 国家编码列表
  /// 说明: 调用此接口,可获取国家编码列表
  Future<PlayerRequestResponse> countriesCodeList() {
    return PlayerRequest(
      url: 'https://interface3.music.163.com/eapi/lbs/countries/v1',
      options: PlayerRequestOptions(
        method: PlayerRequestMethod.post,
        crypto: EncryptType.eapi,
        url: '/api/lbs/countries/v1'
      ),
    ).request();
  }

  /// 签到
  /// 说明 : 调用此接口 , 传入签到类型 ( 可不传 , 默认安卓端签到 ), 可签到 ( 需要登录 ), 其中安卓端签到可获得 3 点经验 , web/PC 端签到可获得 2 点经验
  /// [type]: 签到类型, 默认为安卓签到, ture 为安卓签到, false 为 web/PC 签到
  Future<PlayerRequestResponse> dailyTask({ bool? type }) {
    type = type ?? true;
    return PlayerRequest(
      url: 'https://music.163.com/weapi/point/dailyTask',
      data: { 'type': type ? 0 : 1 },
      options: PlayerRequestOptions(
          method: PlayerRequestMethod.post,
          crypto: EncryptType.weapi,
      ),
    ).request();
  }

  /// 获取订单
  /// 说明: 登录后调用此接口, 可获得订单
  /// [businessType] 订单类型，[ByBusinessType.Album] 数字专辑, [ByBusinessType.MV] 视频
  /// [ByBusinessType.Songs] 单曲, [ByBusinessType.Radio] 播单, [ByBusinessType.Vip] 会员&礼品卡, [ByBusinessType.Tickets] 演出
  Future<PlayerRequestResponse> byBusinessList({ ByBusinessType? businessType, int? limit, int? offset }) {
    Map<String, dynamic> data = {
      'offset': offset ?? 0,
      'limit': limit ?? 30,
      'os': 'iOS'
    };
    businessType = businessType ?? ByBusinessType.Album;
    if (businessType == ByBusinessType.Album) data['business'] = 'Album';
    if (businessType == ByBusinessType.MV) data['business'] = 'MV';
    if (businessType == ByBusinessType.Radio) data['business'] = 'Radio';
    if (businessType == ByBusinessType.Songs) data['business'] = 'Songs';
    if (businessType == ByBusinessType.Tickets) {
      data['business'] = 'Tickets';
      data['type'] = 2;
    }
    if (businessType == ByBusinessType.Vip) {
      data['business'] = 'vip';
      data['type'] = 2;
    }
    return PlayerRequest(
      url: 'https://interface3.music.163.com/eapi/ordering/web/customer/order/bybusiness/list',
      data: data,
      options: PlayerRequestOptions(
        method: PlayerRequestMethod.post,
        crypto: EncryptType.eapi,
        cookie: '',
        url: '/api/ordering/web/customer/order/bybusiness/list'
      ),
    ).request();
  }

  /// 取消订单
  /// 说明: 登录后调用此接口, 可取消当前订单
  /// [orderId] 订单id
  Future<PlayerRequestResponse> closeByBusiness({ required int orderId }) async {
    try {
      PlayerRequestResponse response = await PlayerRequest(
        url: 'https://music.163.com/store/api/order/close?orderId=${orderId.toString()}',
        options: PlayerRequestOptions(
          method: PlayerRequestMethod.get,
        ),
      ).request();
      if (response.status == 200 && int.parse(jsonDecode(response.body)['code'] .toString())== 200) {
        Map<String, dynamic> data = { ...jsonDecode(response.body), 'message': '订单关闭成功', 'orderId': orderId };
        return PlayerRequestResponse(status: 200, body: jsonEncode(data), cookie: response.cookie);
      } else {
        return response;
      }
    } catch (e){
      if (e is PlayerRequestResponse) {
        return e;
      } else {
        return PlayerRequestResponse(status: 400, body: e);
      }
    }
  }

  /// 音乐是否可用
  /// 说明: 调用此接口,传入歌曲 id, 可获取音乐是否可用
  /// [musicId] 音乐ID列表
  /// [br] 码率,默认设置了 999000 即最大码率,如果要 320k 则可设置为 320000,其他类推
  Future<PlayerRequestResponse> checkMusic({ required List<int> musicId, int? br }) {
    if (musicId.length > 100) musicId = musicId.sublist(0, 100);
    return PlayerRequest(
      url: 'https://music.163.com/weapi/song/enhance/player/url',
      data: { 'ids': musicId, 'br': br ?? 999000 },
      options: PlayerRequestOptions(
          method: PlayerRequestMethod.post,
          crypto: EncryptType.eapi,
          url: '/api/ordering/web/customer/order/bybusiness/list'
      ),
    ).request();
  }

  /// 获取歌词
  /// 说明 : 调用此接口 , 传入音乐 id 可获得对应音乐的歌词 ( 不需要登录 )
  /// [id] 音乐 id
  Future<PlayerRequestResponse> lyric({ required int id }) {
    return PlayerRequest(
      url: 'https://music.163.com/weapi/song/enhance/player/url',
      data: { 'id': id, 'lv':-1, 'kv': -1, 'tv': -1 },
      options: PlayerRequestOptions(
          method: PlayerRequestMethod.post,
          crypto: EncryptType.linuxapi,
      ),
    ).request();
  }

  /// 删除动态
  /// 说明 : 登录后调用此接口 ,可以删除用户动态
  /// [evId] 动态 id 必选
  Future<PlayerRequestResponse> eventDel({ required int evId }) {
    Map<String,dynamic> data = { 'id': evId };
    return PlayerRequest(
      url: 'https://music.163.com/eapi/event/delete',
      data: data,
      options: PlayerRequestOptions(
          method: PlayerRequestMethod.post,
          crypto: EncryptType.weapi,
      ),
    ).request();
  }

  /// 转发用户动态
  /// 说明 : 登录后调用此接口 ,可以转发用户动态
  /// [uid] 用户 ID
  /// [evId] 动态 ID
  /// [forwards] 转发的评论
  Future<PlayerRequestResponse> eventForwards({ required int evId, required int uid, required String forwards }) {
    Map<String,dynamic> data = { 'id': evId, 'eventUserId': uid, 'forwards': forwards };
    return PlayerRequest(
      url: 'https://music.163.com/weapi/event/forward',
      data: data,
      options: PlayerRequestOptions(
        method: PlayerRequestMethod.post,
        crypto: EncryptType.weapi,
      ),
    ).request();
  }

  /// 获取动态消息
  /// 说明 : 调用此接口 , 可获取各种动态 , 对应网页版网易云，朋友界面里的各种动态消息 ，如分享的视频，音乐，照片等！
  /// [pageSize] 每页数据,默认 30
  /// [lastTime] 返回数据的 lasttime ,默认-1,传入上一次返回结果的 lasttime,将会返回下一页的数据
  Future<PlayerRequestResponse> event({ int? pageSize, int? lastTime }) {
    Map<String,dynamic> data = { 'pagesize': pageSize ?? 30, 'lasttime': lastTime ?? -1 };
    return PlayerRequest(
      url: 'https://music.163.com/weapi/v1/event/get',
      data: data,
      options: PlayerRequestOptions(
        method: PlayerRequestMethod.post,
        crypto: EncryptType.weapi,
      ),
    ).request();
  }
}
